﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Nirvana
{
    /// <summary>
    /// 定义一个圆形ui点击区域
    /// </summary>
    [RequireComponent(typeof(CircleCollider2D))]
    public class UIBlockCircle : Image
    {
        private CircleCollider2D circle2D;
        private CircleCollider2D Circle2D
        {
            get
            {
                if (circle2D == null) circle2D = GetComponent<CircleCollider2D>();
                return circle2D;
            }
        }

        public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
        {
            if (eventCamera != null)
            {
                Vector3 vector;
                return RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, screenPoint, eventCamera, out vector)
                    && Circle2D.OverlapPoint(vector);
            }
            return Circle2D.OverlapPoint(screenPoint);
        }

#if UNITY_EDITOR
        /// <summary>
        /// 组件菜单Reset命令，复位编辑的多边形为正方形初始状态
        /// </summary>
        protected override void Reset()
        {
            base.Reset();
            transform.localPosition = Vector3.zero;
            Circle2D.radius = rectTransform.sizeDelta.x * 0.5f;

        }
#endif

    }
}

